import java.util.Arrays;

public class test {

    /**
     * 不规则二维数组
     * 二维数组的真正表现形式
     * @param args
     */
    public static void main(String[] args) {
        int[][] array={{1,2,3},{4,5,6}};
        int[][] array2=new int[][]{{1,2,3},{4,5,6}};
        int[][] array3=new int[2][3];

        for (int i = 0; i <2 ; i++) {
            for (int j = 0; j <3 ; j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }

    }

    public static void main11(String[] args) {
        int[] array1={1,2,3,4,5};
        int[] array2={1,2,3,4,5};

        boolean flg=Arrays.equals(array1,array2);
        System.out.println(flg);

        int[] array3=new int[10];
        Arrays.fill(array3,1,3,99);//可局部填充
        System.out.println(Arrays.toString(array3));
    }

    public static void main10(String[] args) {
        int[] array={1,2,3,4,5};
        int[] copy=new int[array.length];
        System.arraycopy(array,0,copy,0,array.length);
        System.out.println(Arrays.toString(copy));
    }

    public static void main9(String[] args) {
        int[] array={1,2,3,4,5};
        /**
         * 参数1 你要拷贝的数组
         * 参数2 代表要拷贝的1数组长度
         */
        int[] copy=Arrays.copyOf(array,array.length*2);//扩容
        int[] copy1=Arrays.copyOfRange(array,1,3);//[1,3)
        System.out.println(Arrays.toString(copy));
        System.out.println(Arrays.toString(copy1));
    }

    public static int[] copy1(int[] array){
        int[] tmp=new int[array.length];
        for (int i = 0; i < array.length ; i++) {
            tmp[i]=array[i];
        }
        return tmp;
    }

    public static void main8(String[] args) {
        int[] array={1,2,3,4,5};

        int[] copy=copy1(array);
        System.out.println(Arrays.toString(copy));

        //以下方式不叫拷贝
        int[] array2={1,2,3,4,5};
        int[] tmp2=array2;
        System.out.println(Arrays.toString(tmp2));
    }

    /**
     * 把数组 转为字符串
     * @param array
     * @return
     */

    public static String myToString(int[] array){
        if(array==null){
            return null;
        }
        if(array.length==0){
            return "[]";
        }

        String ret="[";
        for (int i = 0; i <array.length ; i++) {
            ret+=array[i];
            if(i< array.length-1)
                ret+=",";
        }
        ret+="]";
        return ret;
    }

    public static int find(int[] array,int key){
        for (int i = 0; i < array.length; i++) {
            if(array[i]==key){
                return i;
            }
        }
        return -1;
    }

    //冒泡排序
    public static void bubble(int[] array){
        //i 表示趟数
        for (int i = 0; i < array.length-1 ; i++) {
            boolean flg=false;
            //-i 代表每一趟都比上一趟少1次
            for (int j = 0; j < array.length-1-i ; j++) {
                if(array[j]>array[j+1]){
                    int tmp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=tmp;
                    flg=true;
                }
            }
            if(flg==false){
                break;
            }
        }
    }

    //逆置
    public static void reverse(int[] array){
        int left=0;
        int right= array.length-1;
        while(left<right){
            int tmp=array[left];
            array[left]=array[right];
            array[right]=tmp;
            left++;
            right--;
        }
    }

    public static void main7(String[] args) {
        int[] array={1,21,3,4,10,4,11};
        reverse(array);
        String ret=Arrays.toString(array);
        System.out.println(ret);
    }

    public static void main6(String[] args) {
        int[] array={1,2,21,4,3};
        String ret1=Arrays.toString(array);
        System.out.println(ret1);

        Arrays.sort(array,1,5);//给数组进行排序  还可以进行局部排序 但是左闭右开[1,3)
        String ret=myToString(array);
        System.out.println(ret);
    }

    public static void main5(String[] args) {
        for (int i = 0; i < args.length ; i++) {
            System.out.println(args[i]+" ");
        }
        System.out.println();
    }

    public static void Func(int[] array1){
        for (int i = 0; i <array1.length ; i++) {
            array1[i]*=2;
        }
    }

    public static int[] Func2(int[] array1){
        int[] tmp=new int[array1.length];
        for (int i = 0; i <array1.length ; i++) {
            tmp[i] = array1[i]*2;
        }
        return tmp;
    }

    public static void main4(String[] args) {
        int[] array={1,2,3,4,5};
        int[] retArray=Func2(array);

        String ret=Arrays.toString(retArray);
        System.out.println(ret);

        ret=Arrays.toString(array);
        System.out.println(ret);
    }

    public static void main3(String[] args) {
        int[] array={1,2,3,4,5};
        array=new int[10];
        array=new int[20];
        array=new int[30];
        //最后只会存 最后一个对象的地址
    }

    public static void main2(String[] args) {
        int[] array={1,2,3,4,5};
        for (int i = 0; i < array.length ; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();

        //for each    遍历数组 把每个元素存储到x里面
        for (int x:array){
            System.out.print(x+" ");
        }
        System.out.println();

        //借助java原生的方法  将指定的数组以字符串的形式输出
        String ret=Arrays.toString(array);
        System.out.println(ret);
    }

    public static void main1(String[] args) {
        int[] array={1,2,3,4,5};
        int[] array2=new int[]{1,2,3,4,5};
        int[] array3=new int[10];
        System.out.println(array.length);
        System.out.println(array[0]);
        int[] array4=null;//array4这个引用 不指向任何对象
        System.out.println(array4.length);//求不了长度
        System.out.println(array4[0]);//异常
    }
}
